package com.wan.net.handler.protobuf;

import com.wan.net.core.packet.Packet;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;

public interface Processor {

    int getCmd();

    void process(Channel channel, Packet packet);

    static Processor getServerProcessor(int cmd){
        return HandlerFactory.SERVER_PROCESSOR_MAP.get(cmd);
    }

    static Processor getClientProcessor(int cmd){
        return HandlerFactory.CLIENT_PROCESSOR_MAP.get(cmd);
    }

    class HandlerFactory{
        /** 服务端协议处理器 */
        private final static Map<Integer, Processor> SERVER_PROCESSOR_MAP = new HashMap<>();
        /** 客户端协议处理器 */
        private final static Map<Integer, Processor> CLIENT_PROCESSOR_MAP = new HashMap<>();
        protected static void addServerProcessor(Processor processor){
            SERVER_PROCESSOR_MAP.put(processor.getCmd(),processor);
        }
        protected static void addClientProcessor(Processor processor){
            CLIENT_PROCESSOR_MAP.put(processor.getCmd(),processor);
        }
    }

}
